package com.steven.treatment_nurse.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.steven.treatment_nurse.R;
import com.steven.treatment_nurse.entity.ResultEntity;
import com.steven.treatment_nurse.entity.UserLocation;
import com.steven.treatment_nurse.net.MyCallback;
import com.steven.treatment_nurse.net.TaskNoHead;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

@SuppressLint("ValidFragment")
public class LocationFragment extends LazyLoadBaseFragment {
    @BindView(R.id.mapView)
    MapView map;
    private String batchId;

    public LocationFragment(String batchId) {
        this.batchId = batchId;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_location;
    }

    @Override
    protected void initView(View rootView, Bundle savedInstanceState) {
        map.onCreate(savedInstanceState);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);

    }

    @Override
    protected void requestDataAutoRefresh() {
        super.requestDataAutoRefresh();
        TaskNoHead.getInstance().getLocation(batchId).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response response) {
                try {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(response.body());
                    assert jsonObject != null;
                    if (jsonObject.getInteger("code") == 0) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        List<UserLocation> list = new ArrayList<>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            UserLocation userLocation = new UserLocation();
                            userLocation.setLat(jsonArray.getJSONObject(i).getString("lat"));
                            userLocation.setLon(jsonArray.getJSONObject(i).getString("lon"));
                            list.add(userLocation);
                        }
                        AMap aMap = map.getMap();
                        LatLng latLng = new LatLng(Double.valueOf(list.get(0).getLat()), Double.valueOf(list.get(0).getLon()));
                        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 19));
                        MarkerOptions markerOption = new MarkerOptions();
                        markerOption.position(latLng);
                        markerOption.draggable(true);
                        markerOption.icon(BitmapDescriptorFactory.fromResource(R.mipmap.location));
                        aMap.addMarker(markerOption);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });


//        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 19));

        //设置远小近大效果,设置刷新一次图片资源的周期。

    }

    @Override
    protected void requestData() {
        super.requestData();
        requestDataAutoRefresh();
    }

    //画定位标记图
//    public void drawMarkers() {
//        MarkerOptions markerOptions = new MarkerOptions()
//                .position(new LatLng(latitude, longitude))
//                .icon(BitmapDescriptorFactory.fromResource(R.drawable.daohang_location))
//                .draggable(true);
//        Marker marker = mAMap.addMarker(markerOptions);
//        marker.showInfoWindow();
//    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        if (map != null)
            map.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        map.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        map.onPause();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        map.onSaveInstanceState(outState);
    }
}
